<template>
  <div class="app-container">
    <el-row class="el-row">
      <el-col :span="18">
        <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
      </el-col>
      <el-col :span="6">
        <el-popover
          title="预览式例表格"
          placement="bottom-start"
          width="700"
          trigger="hover">
          <el-image
          src="http://www.alinkeji.com/towerbase.png"
          preview-src-list="http://www.alinkeji.com/towerbase.png">
          </el-image>
          <el-button slot="reference">查看式例表格</el-button>
        </el-popover>
      </el-col>
    </el-row>

    <dynamic-table :data="data" :data-title="dataTitle" v-if="dynamicTableShow"></dynamic-table>
    <dynamic-form v-else></dynamic-form>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalSize">
    </el-pagination>
  </div>
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import DynamicTable from '@/components/Table/DynamicTable.vue'
import { entity } from '@/api/api'
export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent, DynamicTable },
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dynamicTableShow: true,
      dataTitle: [],
      data: []
    }
  },
  created: function () {
    this.getData();
  },
  methods: {
    beforeUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 10
      if (isLt10M) {
        return true
      }
      this.$message({
        message: 'Please do not upload files larger than 10m in size.',
        type: 'warning'
      })
      return false
    },
    handleSuccess(success, data) {
      console.log(success)
      console.log(data)
      success && this.getData()
    },
    getData() {
      return new Promise((resolve, reject) => {
        entity(this.currentPage,this.pageSize,'com.alinkeji.infrastructure.entity.TowerBaseConfig').then(response => {
          const { dataTitle, data } = response
          console.log(data)
          this.dataTitle = dataTitle
          this.data = data.content
          this.totalSize = data.totalElements
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`)
      this.pageSize = pageSize
      this.getData()
    },
    handleCurrentChange(currentPage) {
      console.log(`当前页: ${currentPage}`)
      this.currentPage = currentPage
      this.getData()
    }
  }
}
</script>
